﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Utils;
using MvcEngine.Core;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Repositories;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;

namespace MvcEngine.Gallery.Managers
{
    public class GalleryCategoryManager : IGalleryCategoryManager
    {
        private readonly IGalleryCategoryRepository categoryRepository = null;

        public GalleryCategoryManager(IGalleryCategoryRepository categoryRepository)
        {
            this.categoryRepository = categoryRepository;
        }

        public static IGalleryCategoryManager Current
        {
            get { return IoC.Resolve<IGalleryCategoryManager>(); }
        }

        public GalleryCategory Get(Guid id)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryCategory.GalleryId, id), GalleryCacheKeys.GalleryCategory.Category,
                    () => categoryRepository.Get(id));
        }

        public GalleryCategory Get(string name, Guid galleryId)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryCategory.GalleryName, name, galleryId), GalleryCacheKeys.GalleryCategory.Category,
                    () => categoryRepository.Get(name, galleryId));
        }

        public void Save(GalleryCategory category)
        {
            Guard.ThrowIfEmptyGuid(category.GalleryId);
            bool isUpdated = true;
            using (var unitOfWork = UnitOfWorkHelper.Get())
            {
                GalleryCategory categoryToSave = null;
                if (category.Id != Guid.Empty)
                {
                    categoryToSave = categoryRepository.Get(category.Id);
                }

                if (categoryToSave == null)
                {
                    categoryToSave = new GalleryCategory { Id = Guid.NewGuid() };
                    categoryToSave.CreatedDate = DateTime.Now;
                    isUpdated = false;
                }

                categoryToSave.ModifiedDate = DateTime.Now;
                categoryToSave.Name = category.Name;
                categoryToSave.Slug = category.Slug;
                categoryToSave.Description = category.Description;
                categoryToSave.Keywords = category.Keywords;
                categoryToSave.Visible = category.Visible;
                categoryToSave.GalleryId = category.GalleryId;

                if (isUpdated)
                {
                    categoryRepository.Update(categoryToSave);
                }
                else
                {
                    categoryRepository.Add(categoryToSave);
                }
                unitOfWork.Commit();
            }
            CacheManager.Current.ClearCache(GalleryCacheKeys.GalleryCategory.Category);
        }

        public void Delete(Guid id)
        {
            categoryRepository.Delete(id);
            CacheManager.Current.ClearCache(GalleryCacheKeys.GalleryCategory.Category);
        }

        public IPagination<GalleryCategory> Get(Guid galleryId, int pageIndex, int pageSize)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryCategory.Paging, galleryId, pageIndex, pageSize), GalleryCacheKeys.GalleryCategory.Category,
                () => categoryRepository.Get(galleryId, pageIndex, pageSize));
        }

        public IList<GalleryCategory> GetAllByGallery(Guid galleryId)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryCategory.AllByGallery, galleryId), GalleryCacheKeys.GalleryCategory.Category,
                () => categoryRepository.GetAllByGallery(galleryId));
        }

        public IList<GalleryCategory> GetAllByImage(Guid imageId)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryCategory.ImageCategories, imageId), GalleryCacheKeys.GalleryImage.Category,
                () => categoryRepository.GetAllByImage(imageId));
        }
    }
}
